import Dile_Methods from "@/utils/methods";
import { object_Inf } from "@/utils/typings/index.type";

/*
 * @Descripttion:图表工具类
 * @LastEditors: xzh
 * @LastEditTime: 2024-05-11 02:13:01
 */
export default class Base_Echart {
  /**
   * @Author: xzh
   * @Descripttion: 格式化图表
   * @Param:
   * @param {object_Inf} chartData
   */
  static formatChartData_bar(chartData: object_Inf, param: object_Inf = {}) {
    let { dataArr, xAxis, title, minMaxArr, color } = chartData,
      { cover } = param;
    //获取维度数据
    let legendArr = dataArr?.map((x: object_Inf) => x.name);

    let chartTypeArr = [...new Set(dataArr?.map((x: object_Inf) => x.type))];

    return {
      option: {
        color,
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        legend: {
          bottom: 0,
          data: legendArr,
        },
        grid: {
          top: "3%",
          left: "1%",
          right: "1%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: xAxis,
            axisPointer: {
              type: "shadow",
            },
          },
        ],
        yAxis: chartTypeArr.map((type, index) => {
          //获取后缀
          let { suffix } =
              dataArr
                ?.filter((x: object_Inf) => x.type == type)
                .find((x: object_Inf) => !!x.suffix) || {},
            { min, max } = (minMaxArr || [])[index] || {};
          return {
            type: "value",
            min,
            max,
            axisLabel: {
              formatter: `{value}${suffix || ""}`,
            },
          };
        }),
        series: dataArr?.map((serie: object_Inf, index: number) => {
          let { labelShow, name, type, data, suffix, minMaxIndex } = serie;
          return {
            ...{
              name,
              type: type || "bar",
              yAxisIndex: minMaxIndex,
              tooltip: {
                valueFormatter: (value: string) => {
                  return value;
                },
              },
              data,
            },
            ...(type == "line" && !!suffix
              ? {
                  label: {
                    show: labelShow == false ? false : true,
                    position: index == 0 ? "top" : "bottom",
                    formatter: `{@score} ${suffix}`,
                  },
                }
              : {}),
          };
        }),
        //图表覆盖配置
        ...(cover || {}),
      },
      title,
      id: Dile_Methods.newGuid(),
    };
  }
  /**
   * @Author: xzh
   * @Descripttion: 格式化图表
   * @Param:
   * @param {object_Inf} chartData
   */
  static formatChartData_pie(chartData: object_Inf, param: object_Inf = {}) {
    let { dataArr, xAxis, title, minMaxArr, color } = chartData,
      { cover } = param;
    return {
      option: {
        title: {
          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            type: "pie",
            data: dataArr.map(({ data, name }: object_Inf) => ({
              value: data?.at(0),
              name,
            })),
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
        //图表覆盖配置
        ...(cover || {}),
      },

      title,
      id: Dile_Methods.newGuid(),
    };
  }
}
